<?php
namespace Admin\Controller;
use Think\Controller;
class QuestionController extends CommonController {
	//
    public function index()
    {
      //个数
      $count = M('question')->count();
      $this->assign('num',$count);

        //所有显示出来
        $question = M('question');        
        // 查询满足要求的总记录数
        $count =  $question->count();
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        //问题显示  
        $sql = 'select t1.*,t2.cname,t3.username from question t1 inner join category t2 on t1.cid=t2.id inner join user t3 on t1.uid=t3.id where t1.is_del=1 limit '.$Page->firstRow.','.$Page->listRows;
        $questions = $question->query($sql);

        $this->assign('questions',$questions);        
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    //编辑显示
    public function edit()
    {
        $id = I('get.id');
        $questioninfo = M('question')->where("id=$id")->find();
        $this->assign('questioninfo',$questioninfo);

        $sql = 'select * from category';
        $arr = M()->query($sql);
        $cate = $this->gettree($arr);
        $this->assign('cate',$cate);

        $this->display();
    }
    //编辑
    public function edit_to()
    {
        //print_r($_POST);exit;
        //Array ( [title] => aaaaaaaaaaaaaaaaa [content] => aaaaaaaaaaaaaaa [file] => [cid] => 4 [questionid] => 2 )
        if (IS_POST) {
            $id = I('post.questionid');
            $data['title'] = I('post.title');
            $data['content'] = I('post.content');
            $data['cid'] = I('post.cid');
            $data['update_time'] = time();

            if (strlen($data['title']) < 4 || strlen($data['title']) > 100) {
                $arr = array(
                    'error' => 0,
                    'info' => '标题要4~100个字符串'
                );
               echo json_encode($arr);
               exit;
            }

           $res = M('question')->where(array('id'=>$id))->save($data);
           if ($res) {
               $arr = array(
                    'error' => 1,
                    'info' => '修改成功'
                );
               echo json_encode($arr);
           }else {
                $arr = array(
                    'error' => 0,
                    'info' => '修改失败'
                );
               echo json_encode($arr);
           }
        }

    }

    //编辑器图片上传
    public function img()
    {
        //print_r($_POST);
        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
        $upload->savePath  =      'Uploads/edit/'; // 设置附件上传目录    
        // 上传文件     
        $info   =   $upload->upload();    
        if(!$info) {// 上传错误提示错误信息         
            $arr = array(
            'code' => 0,
            'msg' => $upload->getError(),
            );
            echo json_encode($arr);
 
        }else{// 上传成功   
            $info = current($info);//减维 
            $url =  __ROOT__.'/'.'Uploads/'.$info['savepath'].$info['savename'];    

            $arr = array(
            'code' => 0,
            'msg' => '上传成功',
            'data'=> array(
                'src' => $url,
                'title' => $info['savename']
                )
            );
            echo json_encode($arr);
        }
    }

    //问题置顶 交互
    public function is_zhiding()
    {
        //print_r($_POST);exit;
      if (IS_POST) {
        $questionid = I('post.questionid');
        $sql = 'update question set is_top=0 where id='.$questionid;
        $is_zhiding = M()->execute($sql);
        if ($is_zhiding) {
            $arr = array(
                'error' =>0,
                'info'=>"下顶成功",
                'questionid' => $questionid
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' =>1,
                'info'=>"下顶失败"
                );
            echo json_encode($arr);
        }

      }
        
    }
    //问题未置顶 交互
    public function no_zhiding()
    {
        //print_r($_POST);
      if (IS_POST) {
          $questionid = I('post.questionid');
          $sql = 'update question set is_top=1 where id='.$questionid;
          $no_zhiding = M()->execute($sql);
          if ($no_zhiding) {
              $arr = array(
                  'error' =>0,
                  'info'=>"置顶成功",
                  'questionid' => $questionid
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"置顶失败"
                  );
              echo json_encode($arr);
          }
      }

    }

    //删除显示
    public function del()
    {
      //个数
      $count = M('question')->where('is_del=0')->count();
      $this->assign('num',$count);

        //所有显示出来
        $question = M('question');        
        // 查询满足要求的总记录数
        $count =  $question->count();
        $Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        //问题显示  
      $sql = 'select t1.*,t2.cname,t3.username from question t1 inner join category t2 on t1.cid=t2.id inner join user t3 on t1.uid=t3.id where t1.is_del=0 limit '.$Page->firstRow.','.$Page->listRows;

        $is_del = $question->query($sql);

        $this->assign('is_del',$is_del);        
        $this->assign('page',$show);// 赋值分页输出

    	$this->display();
    }

    //删除状态 为0 
    public function is_del()
    {
        $id = I('post.id');
        $sql = 'update question set is_del=0 where id='.$id;
        $res = M()->execute($sql);
       if ($res) {
           $arr = array(
                'error' => 0,
                'info' => '删除成功'
            );
           echo json_encode($arr);
       }else {
            $arr = array(
                'error' => 1,
                'info' => '删除失败'
            );
           echo json_encode($arr);
       }
    }

    //删除状态为0的用户 恢复  改为1
    public function no_del()
    {
      if (IS_POST) {
          $questionid = I('post.questionid');
          $sql = 'update question set is_del=1 where id='.$questionid;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>0,
                  'info'=>"问题恢复成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"问题恢复失败"
                  );
              echo json_encode($arr);
          }
        }  
    }

    //批量删除
    public function deletes()
    {
      //print_r($_POST);exit;
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);

        $res = M('question')->delete($ids);
          if ($res) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户删除失败"
                  );
              echo json_encode($arr);
          }
      }
    }


}